package com.code.util;

import com.code.constant.ErrorEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.ToString;

/**
 * 封装返回前端格式
 *
 * @author ww
 * @date 2018/8/1
 */
@Getter
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultData<T>{

    /**返回的状态码*/
    private int code;
    /**返回的状态*/
    private String msg;
    /**返回的数据*/
    private T data;

    public ResultData() {
    }

    private ResultData(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private ResultData(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 无返回数据，只有状态码和错误类型
     * Created by ww on 2018/8/2 10:47
     * @return 返回的数据
     */
    public static ResultData error(ErrorEnum errorEnum){
        return new ResultData<>(errorEnum.getCode(), errorEnum.getMsg());
    }

    /**
     * 无返回数据，只有状态
     * Created by ww on 2018/8/2 10:47
     * @return 返回的数据
     */
    public static ResultData success(){
        return new ResultData<>(ErrorEnum.SUCCESS.getCode(), ErrorEnum.SUCCESS.getMsg());
    }

    /**
     * 没有总数返回，只有数据
     * Created by ww on 2018/8/2 10:47
     * @param data 数据
     * @return 返回的数据
     */
    public static <W> ResultData success(W data){
        return new ResultData<>(ErrorEnum.SUCCESS.getCode(), ErrorEnum.SUCCESS.getMsg(), data);
    }

    /**
     * 自定义返回状态码，只有数据
     * Created by ww on 2018/8/2 10:47
     * @param errorEnum 自定义返回状态码
     * @param data 数据
     * @return 返回的数据
     */
    public static <W> ResultData success(ErrorEnum errorEnum, W data){
        return new ResultData<>(errorEnum.getCode(), errorEnum.getMsg(), data);
    }
}
